package com.cennavi.bigdata.baotou.util;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import java.awt.*;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class MapUtils {

	/**
	 * 判断点是否在多边形内
	 * 
	 * @param pointX
	 * @param pointY
	 * @param ptArray
	 * @return
	 */
	public static boolean checkPolygonInner(Point2D.Double point, List<Point2D.Double> polygon) {
		Polygon p = new Polygon();
		final double TIMES = 10000000.0;
		for (Point2D.Double d : polygon) {
			int x = (int) (d.x * TIMES);
			int y = (int) (d.y * TIMES);
			p.addPoint(x, y);
		}
		int x = (int) (point.x * TIMES);
		int y = (int) (point.y * TIMES);
		return p.contains(x, y);
	}

	/**
	 * 判断点是否在圆内
	 * 
	 * @param x
	 *            圆心的经度
	 * @param y
	 *            圆心的纬度
	 * @param x1
	 *            点的经度
	 * @param y1
	 *            点的纬度
	 * @param radius
	 *            圆半径
	 * @return
	 */
	public static String inCircle(double x, double y, double x1, double y1, double radius) {
		double distance1 = (x - x1) * (x - x1) + (y - y1) * (y - y1);
		double distance = Math.sqrt(distance1);
		if (distance > radius) {
			return "圆外";
		} else if (distance == radius) {
			return "圆上";
		} else {
			return "圆内";
		}
	}

	/**
	 * 判断点是否在矩形内
	 * 
	 * @param x1
	 *            矩形顶点的经度
	 * @param y1
	 *            矩形顶点的纬度
	 * @param x2
	 *            矩形顶点的经度
	 * @param y2
	 *            矩形顶点的纬度
	 * @param x3
	 *            点的经度
	 * @param y3
	 *            点的纬度
	 * @return
	 */
	public static String inRect(double x1, double y1, double x2, double y2, double x3, double y3) {
		if (x1 < x3 && x3 < x2 && y1 < y3 && y2 > y3) {
			return "矩形内";
		} else if (x1 == x3 && y2 > y3 && y3 > y1) {
			return "矩形上";
		} else if (y3 == y1 && x1 < x3 && x2 > x3) {
			return "矩形上";
		} else if (x3 == x2 && y2 > y3 && y1 < y3) {
			return "矩形上";
		} else if (y3 == y2 && x3 > x1 && x2 > x3) {
			return "矩形上";
		} else {
			return "矩形外";
		}
	}

	public static String getDeptName(String x, String y) throws NumberFormatException, IOException {
		File f = new File(MapUtils.class.getResource("/ddxq.MIF").getFile());
		// 所有大队点位坐标信息
		List<List<Point2D.Double>> departPoints = Lists.newArrayList();
		List<Point2D.Double> points = Lists.newArrayList();
		for (String tr : FileUtils.readLines(f, "utf-8")) {
			if (tr.contains("Region")) {
				points = Lists.newArrayList();
				departPoints.add(points);
				continue;
			}
			System.out.println(tr);
			points.add(new Point2D.Double(Double.valueOf(tr.replace("\"", "").split(" ")[0]),
					Double.valueOf(tr.replace("\"", "").split(" ")[1])));
		}
		// 所有大队对应名称信息
		List<String> depart = FileUtils.readLines(new File(MapUtils.class.getResource("/ddxq.MID").getFile()),
				"gb2312");
		Point2D.Double point = new Point2D.Double(Double.valueOf(x), Double.valueOf(y));
		for (int i = 0; i < departPoints.size(); i++) {
			List<Point2D.Double> pointss = departPoints.get(i);
			if (MapUtils.checkPolygonInner(point, pointss)) {
				return depart.get(i).replace("\"", "");
			}
		}
		return "";
	}
	
    /**
     * 根据坐标测算两点之间的距离
     * @param jd1
     * @param wd1
     * @param jd2
     * @param wd2
     * @return
     */
	public static double getDistance(double jd1,double wd1,double jd2,  double wd2) {
		double lat1 = (Math.PI / 180) * wd1;
		double lat2 = (Math.PI / 180) * wd2;
		double lon1 = (Math.PI / 180) * jd1;
		double lon2 = (Math.PI / 180) * jd2;
		// 地球半径
		double R = 6371.137;
		double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1)
				* Math.cos(lat2) * Math.cos(lon2 - lon1))
				* R;
		return d*1000 ;
	}
	
	public static void main(String[] args) {
		double aa = getDistance(109.79432106210447, 40.659270520801044, 109.78962122236334, 40.65955297732478);
		System.out.println(aa);
	}
	
}
